from django.http.response import HttpResponse
from django.shortcuts import render
from learn1.models import cfgFile,HostInfo
from django.views.decorators.csrf import csrf_exempt 
from django.contrib import messages

import re

# Create your views here.


def success(request,filename_id):
    # 1.取出文件所有主机清单
    all_hosts = HostInfo.objects.filter(enable=True)
    yamlFile = cfgFile.objects.filter(id=filename_id)
    
    context = {}
    context['yamlfile'] = [a.cfgFile for a in  yamlFile ][0]
    context['all_hosts'] = [ i for i in all_hosts ]
    return render(request, 'learn1/success.html', context)

@csrf_exempt
def putymlfile(request):
    if request.method == "POST":
        hostinfo = request.POST.get('hostinfo', 'None')
        
        yamlfile = request.POST.get('yamlfile', 'None')
        get_cfgFile = cfgFile.objects.filter(cfgFile=yamlfile)
        
        for i in hostinfo.split(","):
            if len(i):
                print(i.split("#")[1])
                get_hostinfo = HostInfo.objects.filter(HostIp=i.split("#")[1])
                print(get_hostinfo)

        return HttpResponse('ddd')
    else:
        return HttpResponse("OK")
